﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UI
{
    public class RecycleBreakupSelectHeroPanel : RecycleSelectPanel 
    {
        public RecycleBreakupSelectPage page;
        public const int selectMax = 4;

        protected string[] selectCache;

        public override void SortItem(List<GameObject> objs)
        {
            objs.Sort(delegate (GameObject x, GameObject y) {
                return -x.GetComponent<RecycleSelectHeroItem>().CompareTo(y.GetComponent<RecycleSelectHeroItem>());
            });
        }

        public override void OnConfirmButtonClick()
        {
            for (int i = 0; i < 4; ++i)
            {
                StaticData.recycleBreakupSelect[i] = selectCache[i];
            }
            page.Close();
        }

        public override void ClearSelectCache()
        {
            selectCache = new string[4];
        }
        public override void InitSelectCache()
        {
            selectCache = new string[4];
            for(int i = 0; i < 4; ++i)
            {
                selectCache[i] = StaticData.recycleBreakupSelect[i]; 
            }
        }

        public override bool IsSelectMax()
        {
            int count = 0;
            for (int i = 0; i < 4; ++i)
            {
                if (!string.IsNullOrEmpty(selectCache[i]))
                {
                    count++;
                }
            }
            return count >= selectMax;
        }

        public override void AddSelectCache(string guid)
        {
            for (int i = 0; i < 4; ++i)
            {
                if (string.IsNullOrEmpty(selectCache[i]))
                {
                    selectCache[i] = guid;
                    return;
                }

            }

            Debug.LogError("Add More In SelectCache");
        }

        public override void RemoveSelectCache(string guid)
        {
            for (int i = 0; i < 4; ++i)
            {
                if (selectCache[i] == guid)
                {
                    selectCache[i] = "";
                    return;
                }
            }
        }

        public override bool IsItemSelected(object item)
        {
            UIHero uiHero = item as UIHero;
            for (int i = 0; i < 4; ++i)
            {
                if (selectCache[i] == uiHero.guid)
                {
                    return true;
                }
            }
            return false;
        }

        public override List<object> GetCanSelectItems()
        {
            List<object> res = new List<object>();
            foreach (var item in StaticData.playerData.InstPlayerData.InstHeros)
            {
                if (item.Value.IsUserHero)
                {
                    continue;
                }
                if (item.Value.heroData.heroClass == (int)HeroClass.Exp_Eater)
                {
                    continue;
                }
                if (StaticData.playerData.InstPlayerData.IsUIHeroInFormation(item.Value))
                {
                    continue;
                }
                if (StaticData.playerData.InstPlayerData.IsHeroInHelpFomation(item.Value.guid))
                {
                    continue;
                }
                res.Add(item.Value);
            }
            return res;
        }

        public override void ShowSelectMaxTip()
        {
            BoxManager.CreatePopupTextBox(TableTextBlo.GetText("recycle_select_breakup_hero_warning"));
        }


        public override GameObject CreateSelectItem(object obj)
        {
            UIHero hero = obj as UIHero;
            GameObject select = null;
            foreach (var item in cachedObjs)
            {
                if (item.GetComponent<RecycleSelectItemBase>().GetItemGUID() == hero.guid)
                {
                    select = item;
                    break;
                }
            }
            if (select != null)
            {
                cachedObjs.Remove(select);
                return select;
            }
            else {
                return GameObject.Instantiate(itemPrefab);
            }
        }

    }
}